<template>
  <div class="page-class">
    <Breadcrumb>
      <a-button type="primary" @click="handleExport">批量导出</a-button>
    </Breadcrumb>
    <div class="cnt-class">
      <div class="query">
        <a-row>
          <a-col :span="6">
            <a-input-search
              v-model.trim="userName"
              :maxLength="50"
              allowClear
              placeholder="请输入楼栋号或门牌号"
              @search="onSearch"
            >
              <a-button slot="enterButton" type="primary"> 搜索 </a-button>
            </a-input-search>
          </a-col>
        </a-row>
        <!-- 筛选列表 -->
        <Collapse :opend="opend" @changOpend="changOpend">
          <a-row>
            <a-col :span="6">
              <span class="collapse-label">楼栋：</span>
              <a-select v-model="query.build" placeholder="请选择楼栋">
                <a-select-option
                  v-for="(node, index) in buildings"
                  :key="index"
                  :value="node.value"
                  >{{ node.name }}</a-select-option
                >
              </a-select>
            </a-col>

            <a-col :span="6">
              <span class="collapse-label">门牌号：</span>
              <a-select v-model="query.number" placeholder="请选择楼栋">
                <a-select-option
                  v-for="(node, index) in houseNums"
                  :key="index"
                  :value="node.value"
                  >{{ node.name }}</a-select-option
                >
              </a-select>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24">
              <a-button type="primary" @click="getList(2)">查询</a-button>
              <a-button type="default" @click="handleRest">重置</a-button>
            </a-col>
          </a-row>
        </Collapse>
      </div>
      <div class="table">
        <BaseTable
          ref="table"
          rowKey="id"
          :scroll="{ y: y }"
          :loading="loading"
          :columns="columns"
          :dataSource="list"
          :hideSelect="true"
          @change="getList"
        ></BaseTable>
      </div>
    </div>
    <Unbind :visible="visible" :data="rows" @close="handleClose"></Unbind>
  </div>
</template>
<script>
import BaseTable from "@/components/BaseTable";
import Collapse from "@/components/Collapse";
import httpApi from "@/api/log";
import Unbind from "./components/Unbind";

export default {
  components: {
    BaseTable,
    Collapse,
    Unbind
  },
  data() {
    return {
      loading: false,
      opend: true,
      query: {
        build: undefined,
        number: undefined
      },
      userName: undefined,
      buildings: [
        {
          name: "sdf",
          value: 1
        }
      ],
      houseNums: [
        {
          name: "sdf",
          value: 1
        }
      ],
      visible: false,
      rows: [],
      time: -1,
      list: [],
      columns: [
        {
          title: "编号",
          dataIndex: "userNo",
          ellipsis: true
        },
        {
          title: "楼栋号",
          dataIndex: "userName",
          ellipsis: true
        },
        {
          title: "门牌号",
          dataIndex: "type",
          ellipsis: true
        },
        {
          title: "产权面积",
          dataIndex: "mobile",
          ellipsis: true,
          customRender: (data = 0) => {
            return data + "㎡";
          }
        },
        {
          title: "业主姓名",
          dataIndex: "userName",
          ellipsis: true
        },
        {
          title: "业主手机号",
          dataIndex: "userName",
          ellipsis: true
        },
        {
          title: "业主身份证号",
          dataIndex: "userName",
          ellipsis: true
        },
        {
          title: "操作",
          eclipse: true,
          customRender: (v, row) => {
            return (
              <div class="operate-td" v-auth="community.unbind">
                <a onClick={() => this.handleUnbind(row)}>解绑业主</a>
              </div>
            );
          }
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  computed: {
    y() {
      return this.opend ? 370 : 480;
    }
  },
  methods: {
    changOpend(opend) {
      this.opend = opend;
    },
    handleRest() {
      // 重置
      this.query = {
        build: undefined,
        number: undefined
      };
    },
    onSearch() {
      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.getList(1);
      }, 600);
    },
    getList(current) {
      this.loading = true;
      if (current === 1) {
        this.$refs.table.pageInfo.current = 1;
      }
      httpApi
        .getList({
          userName: this.userName,
          pageNo: this.$refs.table.pageInfo.current,
          pageSize: this.$refs.table.pageInfo.pageSize
        })
        .then((res) => {
          if (res.retcode === 0) {
            this.list = res.data.records || [];
            if (this.$refs.table) {
              this.$refs.table.pageInfo.total = res.data.total;
            }
          } else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.error(err.msg);
          this.loading = false;
        });
    },
    handleExport() {
      //批量导出
      this.$confirm({
        title: "批量导出：",
        content: () => <div class="pwd-msg">是否确定批量导出?</div>,
        onOk: () => {
          httpApi
            .resetPwd()
            .then((res) => {
              if (res.retcode === 0) {
                this.$message.success("导出成功！");
              } else {
                this.$message.error("导出失败！");
              }
            })
            .catch((err) => {
              this.$message.error(err.msg || "导出失败！");
            });
        },
        onCancel: () => {}
      });
    },
    handleUnbind(row) {
      //解绑业主
      this.visible = true;
      this.rows = [row];
    },
    handleClose(show) {
      this.visible = show;
      this.rows = [];
    }
  }
};
</script>
<style lang="less" scoped>
/deep/.ant-row .ant-col {
  display: flex;
  align-items: center;
  .ant-select {
    flex: 1;
  }
}
</style>
